package newcoder_exp.justPractise;

import java.util.*;
import org.junit.*;

public class NC38 {
    public class Solution {
        public ArrayList<Integer> spiralOrder(int[][] matrix) {
            ArrayList<Integer> res = new ArrayList<>();
            if (matrix == null || matrix.length == 0)
                return res;
            int m = matrix.length, n = matrix[0].length;
            int l = 0, b = m - 1, r = n - 1, u = 0;
            while (l <= r && u <= b) {
                for (int i = l; i < r; i++) {
                    res.add(matrix[u][i]);
                }
                for (int i = u; i < b; i++) {
                    res.add(matrix[i][r]);
                }
                for (int i = r; i > l; i--) {
                    res.add(matrix[b][i]);
                }
                for (int i = b; i > u; i--) {
                    res.add(matrix[i][l]);
                }
                l++;
                u++;
                r--;
                b--;
            }
            if (m == n && (m & 1) == 1) {
                res.add(matrix[l][l]);
            }
            return res;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[][] nums = {new int[]{7,8,9}};
        System.out.println(s.spiralOrder(nums));
    }
}
